# coding=utf-8
"""
作者：董新强 
创建时间：2020-5-9
描述：微信用户管理
"""
from tornado.gen import coroutine
from tornado.httpclient import HTTPRequest, AsyncHTTPClient, HTTPError

from auth.utils import to_dict
from wx.wx_base import WxBase


class WxUserManage(WxBase):
    def __init__(self):
        super().__init__()

    @coroutine
    def jscode2session(self, code):
        url = 'https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code'.format(self.app_mini_id, self.app_mini_secret, code)
        request = HTTPRequest(url, method='GET')
        client = AsyncHTTPClient()
        try:
            res = yield client.fetch(request)
            return to_dict(res.body)
        except HTTPError as e:
            print('jscode2session=>', e)
        except Exception as e:
            print('jscode2session=>', e)
        return None


if __name__ == '__main__':
    pass
